#!/bin/bash

function check(){
  if [ ! -d "nginx" ];then
    echo "未安装nginx,请执行install脚本安装"
    exit
  fi
}
function start(){
    check
    if [ -d .env ];then
      . .env/bin/activate
    fi
    ## 启动django 和 websockify
    cd vmbox-api/
    nohup python3 run.py &> ./logs.log &
    cd ../ws-scrcpy
    nohup node dist/index.js &> ./logs.log &
    ## 启动nginx
    cd ../nginx
    ./sbin/nginx
    cd ..
}

function stop() {
    check
    ps aux|grep -E 'python3 run.py|python -m websockify|scrcpy'  |grep -v 'grep' | awk '{print "kill "$2}' | bash
    cd nginx
      ./sbin/nginx -s stop
    cd ..
}

function ng() {
  cd nginx
  if [ "$1" == "start" ];then
    ./sbin/nginx
  else
    ./sbin/nginx -s $1
  fi

}

case $1 in
start)
    start
    echo "[start OK]"
    exit
  ;;
esac
case $1 in
stop)
    stop
    echo "[stop OK]"
    exit
  ;;
esac
case $1 in
restart)
    stop
    echo "[stop OK]"
    start
    echo "[start OK]"
    echo "[restart OK]"
    exit
  ;;
ng)
  ng $2
  echo "[nginx OK]"
  exit
  ;;
esac

echo -e '\n请输入参数!\n \ncommand: start.sh [start|stop|restart|ng ${nginxOption}]\n\t\tginxOption: \n\t\t\t [stop|restart]'